package com.nh.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FileUploadAction extends ActionSupport {
    private File file;
    private String fileFileName;
    private String uuid;
    private String imgUrl;
    private String fileFileContentType;
    private String message = "成功上传文件";
    private String state;
    private Long fsize;
    //private IBeanFileService fileService;
	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
	public Long getFsize() {
		return fsize;
	}

	public void setFsize(Long fsize) {
		this.fsize = fsize;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	

	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileFileContentType() {
        return fileFileContentType;
    }

    public void setFileFileContentType(String fileFileContentType) {
        this.fileFileContentType = fileFileContentType;
    }

    @SuppressWarnings("deprecation")
    @Override
    public String execute() throws Exception {
        String path = ServletActionContext.getRequest().getRealPath("/images");
        try {
            File f = this.getFile();
            this.setFsize(f.length());
            String oFileName=this.getFileFileName();
            String[] sf=oFileName.split("\\.");
            if(!isLegal(oFileName)){
            	this.setState("error");
                message="格式出错";
                return ERROR;
            }
            FileInputStream inputStream = new FileInputStream(f);
            String uuid=createUUID();
            String nFileName=uuid+"."+sf[1];
            this.setImgUrl("images/"+nFileName);
            this.setUuid(uuid);
            addFile(uuid,"images/"+nFileName);
            FileOutputStream outputStream = new FileOutputStream(path + "\\"+nFileName);
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, length);
            }
            this.setState("success");
            inputStream.close();
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
            this.setState("error");
            message = "对不起,上传失败";
        }
        return SUCCESS;
    }
    // 添加到文件
    private void addFile(String uuid,String url){
    	/*
    	BeanFile bean=new BeanFile();
    	bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
    	bean.setFilename(this.getFileFileName());
    	bean.setContenttype(this.getFileFileContentType());
    	bean.setUrl(url);
    	bean.setUuid(uuid);
    	bean.setFsize(this.getFsize());
    	fileService.save(bean);
    	*/
    }
    //创建uuid
    private String createUUID(){
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}
    //判断格式是否合法
    private boolean isLegal(String fileName){
    	String[] formats={".exe"};
    	boolean flag=true;
    	for(int i=0;i<formats.length;i++){
    		if(fileName.endsWith(formats[i])){
    			flag=false;
    		}
    	}
    	return flag;
    }
}